package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:ProductUnitController
 * Package:com.spzx.product.controller
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/20 - 11:26
 */
@Tag(name = "商品单位接口管理")
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private ProductUnitService productUnitService;

    /**
     * 新增商品单位
     *
     * @param productUnit
     * @return
     */
    @PostMapping("")
    public AjaxResult addProductUnit(@RequestBody ProductUnit productUnit) {
        productUnit.setDelFlag("0");
        boolean save = productUnitService.save(productUnit);
        return toAjax(save);
    }

    /**
     * 修改商品单位
     *
     * @param productUnit
     * @return
     */
    @PutMapping("")
    public AjaxResult updateProductUnit(@RequestBody ProductUnit productUnit) {
        productUnit.setDelFlag("0");
        boolean save = productUnitService.updateProductUnit(productUnit);
        return toAjax(save);
    }

    /**
     * 获取商品单位详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getProductUnitById(@PathVariable Long id) {
        ProductUnit productUnit = productUnitService.getById(id);
        return AjaxResult.success(productUnit);
    }

    /**
     * 获取分页列表
     *
     * @param productUnit
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo getPageList(ProductUnit productUnit) {
        startPage();
        List<ProductUnit> productUnitList =
                productUnitService.getPageList(productUnit);
        return getDataTable(productUnitList);
    }

    /**
     * 获取全部单位
     * @return
     */
    @GetMapping("/getUnitAll")
    public TableDataInfo getUnitAll() {
        List<ProductUnit> productUnitList = productUnitService.list(Wrappers.lambdaQuery(ProductUnit.class)
                .eq(ProductUnit::getDelFlag, "0"));
        return getDataTable(productUnitList);
    }

    /**
     * 删除商品单位
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public AjaxResult deleteByIds(@PathVariable Long[] ids) {
        boolean b = productUnitService.deleteByIds(ids);
        return toAjax(b);
    }

}
